<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>02使用百度地图</title>
  <link rel="stylesheet" href="../ol/ol.css">
  <style>
    #map {
      width: 400px;
      height: 400px;
    }
  </style>
</head>
<body>
<div id="map"></div>

<script src="../ol/ol.js"></script>
<script>

  // 参考坐标系
  let projection = ol.proj.get('EPSG:3857')
  // 分辨率
  let resolutions = []
  for(var i = 0; i < 19; i++) {
    resolutions[i] = Math.pow(2, 18 - i)
  }

  let tilegrid = new ol.tilegrid.TileGrid({
    origin: [0, 0],
    resolutions: resolutions
  })

  // 连接百度地图的瓦片地址请求地址
  let baidu_source = new ol.source.TileImage({
    // 设置坐标系
    projection,
    // 设置分辨率
    tileGrid: tilegrid,
    // 百度地图的瓦片图请求地址
    tileUrlFunction: function(tileCoord, pixelRatio, proj) {
      if (!tileCoord) {
        return ""
      }
      var z = tileCoord[0]
      var x = tileCoord[1]
      var y = tileCoord[2]
      if (x < 0) {
        x = "M" + (-x)
      }
      if (y < 0) {
        y = "M" + (-y)
      }
      return "http://online3.map.bdimg.com/onlinelabel/?qt=tile&x=" + x + "&y=" + y + "&z=" + z + "&styles=pl&udt=20151021&scaler=1&p=1"
    }
  })

  const map = new ol.Map({
    target: 'map',
    // 图层
    layers: [
      // 加载瓦片图
      new ol.layer.Tile({
        // 百度图源
        source: baidu_source
      })
    ],
    view: new ol.View({
      center: [113.267252, 23.137949], // 设置视图中心点
      zoom: 2, // 默认缩放级别
    })
  })
</script>
</body>
</html>